//
//  TabviewViewModel.m
//  txlc_ios
//
//  Created by ayi on 2018/10/7.
//  Copyright © 2018 txlc. All rights reserved.
//

#import "TabviewViewModel.h"


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprotocol"

@implementation TabviewViewModel

- (NSMutableArray *)listAry{if(!_listAry)_listAry = [NSMutableArray arrayWithCapacity:0];return _listAry;}



#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_numberOfSectionsInTableView:)]) {
        return [self.delegate vm_numberOfSectionsInTableView:tableView];
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:numberOfRowsInSection:)]) {
        return [self.delegate vm_tableView:tableView numberOfRowsInSection:section];
    }
    return self.listAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:cellForRowAtIndexPath:)]) {
        return [self.delegate vm_tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellDefault"];
    if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellDefault"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}




#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:heightForRowAtIndexPath:)]) {
        return [self.delegate vm_tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    return self.rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:didSelectRowAtIndexPath:andModel:)]) {
        if (self.listAry.count == 0) {
            return;
        }
        NSInteger allsection = [tableView numberOfSections];
        id model;
        if (allsection > 1) {
            model = self.listAry[indexPath.section][indexPath.row];
        }else{
            id checkmodel = self.listAry[0];
            if ([checkmodel isKindOfClass:[NSArray class]]) {
                model = self.listAry[indexPath.section][indexPath.row];
            }else{
                model = self.listAry[indexPath.row];
            }
        }
        [self.delegate vm_tableView:tableView didSelectRowAtIndexPath:indexPath andModel:model];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:viewForHeaderInSection:)]) {
        return [self.delegate vm_tableView:tableView viewForHeaderInSection:section];
    }
    if (self.sectionHeaderHeight != 0) {
        UIView *header = [[UIView alloc]init];header.backgroundColor = self.sectionHeaderColor?:[UIColor whiteColor];
        return header;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:viewForFooterInSection:)]) {
        return [self.delegate vm_tableView:tableView viewForFooterInSection:section];
    }
    if (self.sectionFooterHeight != 0) {
        UIView *footer = [[UIView alloc]init];footer.backgroundColor = self.sectionFooterColor?:[UIColor whiteColor];
        return footer;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:heightForHeaderInSection:)]) {
        return [self.delegate vm_tableView:tableView heightForHeaderInSection:section];
    }
    if (section == 0) {
        return .5f;
    }
    return self.sectionHeaderHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_tableView:heightForFooterInSection:)]) {
        return [self.delegate vm_tableView:tableView heightForFooterInSection:section];
    }
    return self.sectionFooterHeight;
}

#pragma mark ----------------------------------------------------------------------------------------------------scrollviewdelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(vm_scrollViewDidScroll:)]) {
        [self.delegate vm_scrollViewDidScroll:scrollView];
    }
}






#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------UICollectionViewDelegate





#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------UICollectionViewDataSource



@end




#pragma clang diagnostic pop
